package com.syezon.wifikey.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtil {

    private static final String DEFAULT_DATE_FORMAT = "yyyyMMdd";

    /**
     * 把日期型转换成字符串形式。
     * 
     * @param date 日期
     * @param dateFormat 日期格式，例如"yyyy-MM-dd"、"yyyy年MM月dd"
     * @return 日期字符串
     */
    public static String toLocaleString(Date date, String dateFormat) {
        if (date == null) {
            return "";
        }

        if (null == dateFormat || "".equals(dateFormat.trim())) {
            return new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(date);
        }
        return new SimpleDateFormat(dateFormat).format(date);
    }

    /**
     * 返回 20110605 类似的整数
     * 
     * @param date
     * @return
     */
    public static int getDayInt(Date date) {
        String s = toLocaleString(date, "yyyyMMdd");
        return Integer.parseInt(s);
    }

    /**
     * 返回 2011060511 类似的整数
     * 
     * @param date
     * @return
     */
    public static int getDayHourInt(Date date) {
        String s = toLocaleString(date, "yyyyMMddHH");
        return Integer.parseInt(s);
    }

    /**
     * 返回类似235959的整数
     * 
     * @param date
     * @return
     */
    public static int getTimeInt(Date date) {
        String s = toLocaleString(date, "HHmmss");
        return Integer.parseInt(s);
    }

    /**
     * 返回 2011060511 类似的字符串
     * 
     * @param date
     * @return
     */
    public static String getDayHourStr(Date date) {
        return toLocaleString(date, "yyyyMMddHH");
    }

    /**
     * 返回 yyyyMMddHHmmss 格式的long类型
     * 
     * @param date
     * @return
     */
    public static long getDaySecondLong(Date date) {
        String s = toLocaleString(date, "yyyyMMddHHmmss");
        return Long.parseLong(s);
    }

    /**
     * 返回 yyyyMMdd 格式的String类型
     * 
     * @param date
     * @return
     */
    public static String getDayStr(Date date) {
        return toLocaleString(date, "yyyyMMdd");
    }

    /**
     * 把日期型转换成"yyyy-MM-dd"字符串形式。
     * 
     * @param date
     * @return 日期字符串
     */
    public static String toLocaleString(Date date) {
        return toLocaleString(date, null);
    }

    /**
     * 获得sysdate+hours后的时间
     * 
     * @param hours 提前或者推后的时间
     * @return sysdate+hours后的时间
     */
    public static Date getSysDateAddHours(int hours) {
        Calendar time = Calendar.getInstance();

        time.add(Calendar.HOUR, hours);

        return time.getTime();
    }

    /**
     * 获得sysdate+hours后的时间
     * 
     * @param days 提前或者推后的时间
     * @return sysdate+hours后的时间
     */
    public static Date getSysDateAddDays(int days) {
        Calendar time = Calendar.getInstance();
        time.add(Calendar.DATE, days);
        return time.getTime();
    }

    /**
     * 取得系统当前日期
     */
    public static Date getCurrentTime() {
        return new Date();
    }

    /**
     * 返回多少时间的前的时间, seconds 可以是负的
     * 
     * @param seconds
     */
    public static Date addTime(Date date, int seconds) {
        Calendar c = Calendar.getInstance();

        c.setTime(date);
        c.add(Calendar.SECOND, seconds);

        return c.getTime();
    }

    public static Date addDay(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, days);
        return c.getTime();
    }

    /**
     * @param date
     * @param pattern "yyyy-MM-dd HH:mm:ss.SSS"
     * @return
     */
    public static String dateToStr(Date date, String pattern) {
        DateFormat ymdhmsFormat = new SimpleDateFormat(pattern);
        return ymdhmsFormat.format(date);
    }

    /**
     * 获得当天日期
     * 
     * @date 2009-2-20
     */
    public static Date getToday() {
        Calendar ca = Calendar.getInstance();
        return ca.getTime();
    }

    /**
     * @param str
     * @param pattern "yyyy-MM-dd HH:mm:ss.SSS"
     * @return
     * @throws ParseException
     */
    public static Date strToDate(String str, String pattern) {
        if (pattern == null) {
            pattern = "yyyy-MM-dd HH:mm:ss.SSS";
        }
        DateFormat ymdhmsFormat = new SimpleDateFormat(pattern);
        try {
            return ymdhmsFormat.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * @date 2009-2-20
     */
    public static Date mkDate(int year, int month, int date) {
        Calendar ca = Calendar.getInstance();
        ca.set(year, month - 1, date);
        return ca.getTime();
    }

    /**
     * 获取当月从1号至当前的日期字符串集合
     * 
     * @return
     */
    public static List<String> currentMonthDays() {
        List<String> list = new ArrayList<String>();
        Calendar ca = Calendar.getInstance();
        int today = ca.get(Calendar.DAY_OF_MONTH);
        int firstDay = 1;
        while (today >= firstDay) {
            ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH), today);
            Calendar agoDate = new GregorianCalendar(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),
                                                     ca.get(Calendar.DATE));
            String dayStr = toLocaleString(agoDate.getTime(), "yyyy-MM-dd");
            today -= 1;
            list.add(dayStr);
        }
        return list;
    }

    /**
     * 获取指定月从1号至当前月底的日期字符串集合
     * 
     * @return
     */
    @SuppressWarnings("static-access")
    public static List<String> getMonthDays(int month) {
        List<String> list = new ArrayList<String>();
        Calendar ca = Calendar.getInstance();
        int curMonth = ca.get(Calendar.MONTH) + 1;
        if (month == curMonth) {
            return currentMonthDays();
        }
        Calendar agoDate = new GregorianCalendar(ca.get(Calendar.YEAR), month - 1, 1);
        int lastD = agoDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        int firstDay = 1;
        while (lastD >= firstDay) {
            ca.set(agoDate.get(Calendar.YEAR), agoDate.get(agoDate.MONTH), lastD);
            Calendar lsDate = new GregorianCalendar(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),
                                                    ca.get(Calendar.DATE));
            String dayStr = toLocaleString(lsDate.getTime(), "yyyy-MM-dd");
            lastD -= 1;
            list.add(dayStr);
        }
        return list;
    }

    /**
     * 获取指定月从指定号至当前月底的日期字符串集合
     * 
     * @return
     */
    @SuppressWarnings("static-access")
    public static List<String> getMonthDays(int month, int firstDay) {
        List<String> list = new ArrayList<String>();
        Calendar ca = Calendar.getInstance();
        int curMonth = ca.get(Calendar.MONTH) + 1;
        if (month == curMonth) {
            return currentMonthDays();
        }
        Calendar agoDate = new GregorianCalendar(ca.get(Calendar.YEAR), month - 1, 1);
        int lastD = agoDate.getActualMaximum(Calendar.DAY_OF_MONTH);
        while (lastD >= firstDay) {
            ca.set(agoDate.get(Calendar.YEAR), agoDate.get(agoDate.MONTH), lastD);
            Calendar lsDate = new GregorianCalendar(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),
                                                    ca.get(Calendar.DATE));
            String dayStr = toLocaleString(lsDate.getTime(), "yyyy-MM-dd");
            lastD -= 1;
            list.add(dayStr);
        }
        return list;
    }

    /**
     * 获取某月最后一天的号
     * 
     * @param date
     * @return
     */
    public static Date lastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    public static Date getWeekAgo() {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, -7);
        return ca.getTime();
    }

    public static Date getWeekAfter(int day) {
        return getAnotherDay(day, 7);
    }

    /**
     * @Title: getNextDay
     * @Description: 获取输入日期的后一天
     * @param day
     * @return Date
     * @throws
     */
    public static Date getNextDay(int day) {
        return getAnotherDay(day, 1);
    }

    public static Date getAnotherDay(int day, int period) {
        Date date = strToDate(Integer.toString(day), DEFAULT_DATE_FORMAT);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, period);
        return ca.getTime();
    }

    /**
     * 计算两个日期之间相差的天数,date2Str-date1Str ,参数为日期字符串 yyyyMMdd
     * 
     * @param date1Str
     * @param date2Str
     * @return
     * @throws ParseException
     */
    public static int getIntervalDays(String date1Str, String date2Str) throws ParseException {
        SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
        Date date1 = sim.parse(date1Str);
        Date date2 = sim.parse(date2Str);

        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();

        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获取指定日期的明天对应的日期的字符串 yyyyMMdd
     * 
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static String getNextDayStr(String dateStr) throws ParseException {
        SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
        Date date = sim.parse(dateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 1);
        return sim.format(cal.getTime());
    }

    // 获取前天日期
    public static Date getBeforeYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_YEAR, -2);
        return cal.getTime();
    }

    // 获取昨天日期
    public static Date getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_YEAR, -1);
        return cal.getTime();
    }

    /**
     * 毫秒转换为日期,返回日期字符出演
     * 
     * @param ms
     */
    public static String convertMsToDate(Long ms) {
        Date dat = new Date(ms);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dat);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = format.format(gc.getTime());
        return dateStr;
    }

    /**
     * 获得当月1号日期
     * 
     * @return
     */
    public static Date firstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取date对应的是星期几
     * 
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取opDate对应的日期与星期
     * 
     * @return "yyyy/MM/dd 星期"
     */
    public static String getDateAndDayOfWeekStr(Integer opDate) {
        Date date = strToDate(String.valueOf(opDate), "yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week = cal.get(Calendar.DAY_OF_WEEK);
        String result = dateToStr(date, "yyyy/MM/dd ");
        switch (week) {
            case 1:
                result += "日";
                break;
            case 2:
                result += "一";
                break;
            case 3:
                result += "二";
                break;
            case 4:
                result += "三";
                break;
            case 5:
                result += "四";
                break;
            case 6:
                result += "五";
                break;
            case 7:
                result += "六";
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 获取dayOfWeek对应星期
     * 
     * @return "星期几"
     */
    public static String getDayOfWeekStr(int dayOfWeek) {
        String result = "星期";
        switch (dayOfWeek) {
            case 1:
                result += "日";
                break;
            case 2:
                result += "一";
                break;
            case 3:
                result += "二";
                break;
            case 4:
                result += "三";
                break;
            case 5:
                result += "四";
                break;
            case 6:
                result += "五";
                break;
            case 7:
                result += "六";
                break;
            default:
                break;
        }
        return result;
    }

    public static Date getDayOfNextMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取上个月的月份
     * 
     * @return
     */
    public static int getLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        return Integer.parseInt(dateToStr(cal.getTime(), "yyyyMM"));
    }

    /**
     * 获取某月最后一天的号
     * @return
     */
    public static Date lastDayOfMonth(Integer month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(strToDate(String.valueOf(month), "yyyyMM"));
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取下一天的零点时间戳 (unix timestamp)
     */
    public static long getNextDayZeroTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTimeInMillis() / 1000;
    }

    public static void main(String[] args) {
        String a = "a";
        a += "b";
        System.out.println(a);
    }
}
